{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Python\n",
    "by Maxwell Margenot\n",
    "\n",
    "Part of the Quantopian Lecture Series:\n",
    "\n",
    "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
    "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
    "\n",
    "Notebook released under the Creative Commons Attribution 4.0 License.\n",
    "\n",
    "---\n",
    "\n",
    "All of the coding that you will do on the Quantopian platform will be in Python. It is also just a good, jack-of-all-trades language to know! Here we will provide you with the basics so that you can feel confident going through our other lectures and understanding what is happening."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python入门\n",
    "by Maxwell Margenot\n",
    "\n",
    "Part of the Quantopian Lecture Series:\n",
    "\n",
    "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
    "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
    "\n",
    "Notebook released under the Creative Commons Attribution 4.0 License.\n",
    "\n",
    "---\n",
    "\n",
    "你将在Quantopain平台的python环境中执行所有的代码.Python是一门十分优秀、几乎万能的编程语言.这里将会传授一些基础知识,以便大家能在接下来的课程中更加有信心,也能明白课程中在讲什么."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code Comments\n",
    "\n",
    "A comment is a note made by a programmer in the source code of a program. Its purpose is to clarify the source code and make it easier for people to follow along with what is happening. Anything in a comment is generally ignored when the code is actually run, making comments useful for including explanations and reasoning as well as removing specific lines of code that you may be unsure about. Comments in Python are created by using the pound symbol (`# Insert Text Here`). Including a `#` in a line of code will comment out anything that follows it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代码注释\n",
    "\n",
    "一个代码注释指的是程序员在源代码中插入的一个笔记.是对源代码的说明,让人更容易明白接下来会发生什么.当代码实际运行时,注释内容是被忽略的,做注释能让代码更具可读性和有条理,当不确定是否删除某些代码行时,注释会有帮助.行注释在Pyhon中以一个`#`开头,后面接你想要输入任何文字. (`# 在这里插入文本`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# This is a comment\n",
    "# These lines of code will not change any values\n",
    "# Anything following the first # is not run as code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may hear text enclosed in triple quotes (`\"\"\" Insert Text Here \"\"\"`) referred to as multi-line comments, but this is not entirely accurate. This is a special type of `string` (a data type we will cover), called a `docstring`, used to explain the purpose of a function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可能听过用三对双引号`(\"\"\" 在这里插入文本 \"\"\")`来表示多行的注释,这个说法不完全准确,这是一种特殊的`string`(后面会讲到这种数据类型)数据类型,叫做`docstring`,用来解释函数的用法."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" This is a special string \"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure you read the comments within each code cell (if they are there). They will provide more real-time explanations of what is going on as you look at each line of code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请确保阅读每个`code cell`中的注释(如果有的话).他们会提供代码实时的说明."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables\n",
    "\n",
    "Variables provide names for values in programming. If you want to save a value for later or repeated use, you give the value a name, storing the contents in a variable. Variables in programming work in a fundamentally similar way to variables in algebra, but in Python they can take on various different data types.\n",
    "\n",
    "The basic variable types that we will cover in this section are `integers`, `floating point numbers`, `booleans`, and `strings`. \n",
    "\n",
    "An `integer` in programming is the same as in mathematics, a round number with no values after the decimal point. We use the built-in `print` function here to display the values of our variables as well as their types!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量\n",
    "\n",
    "变量为编程中的值提供名字.如果你想把某个值保存下来以供稍后或重复使用,你可以给这个值起一个名字,并将其内容储存在变量中.程序中的变量和代数中的变量很相似,在Python中,他们可以是各种不同的数据类型.\n",
    "\n",
    "在接下来的章节我们会介绍一些基础的变量类型,包括`integers(整数)`,`float point number(浮点数)`, `booleans(布尔值)`, 和 `strings(字符串)`.\n",
    "\n",
    "编程中的`integer` 意义和数学里一样, 表示一个没有小数点的数.我们用内置函数`print`来展示变量的值及变量类型！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_integer = 50\n",
    "print(my_integer, type(my_integer))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables, regardless of type, are assigned by using a single equals sign (`=`). Variables are case-sensitive so any changes in variation in the capitals of a variable name will reference a different variable entirely."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "变量,可以无视数据类型只用一个等号(`=`)来表示赋值.变量名对大小写敏感,变量名称的任何大小写的变化都将使他变成另外一个变量."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "one = 1\n",
    "print(one)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `floating point` number, or a `float` is a fancy name for a real number (again as in mathematics). To define a `float`, we need to either include a decimal point or specify that the value is a float."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个浮点数,其实是实数的\"花名\"(这里的含义和数学中一样).定义一个浮点数,只需要加上一个小数点,或者指定类型为浮点数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "my_float = 1.0\n",
    "print(my_float, type(my_float))\n",
    "my_float = float(1)\n",
    "print(my_float, type(my_float))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A variable of type `float` will not round the number that you store in it, while a variable of type `integer` will. This makes `floats` more suitable for mathematical calculations where you want more than just integers.\n",
    "\n",
    "Note that as we used the `float()` function to force an number to be considered a `float`, we can use the `int()` function to force a number to be considered an `int`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`float`类型并不会对你存储的数字进行舍入,`integer`类型则会.相较于`int`类型,`float`类型更适合科学计算.\n",
    "\n",
    "注意:我们可以使用`float()` 函数强制将数字转成一个`float`,亦可用`int()`函数强制将数字转换成`int`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_int = int(3.14159)\n",
    "print(my_int, type(my_int))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `int()` function will also truncate any digits that a number may have after the decimal point!\n",
    "\n",
    "Strings allow you to include text as a variable to operate on. They are defined using either single quotes ('') or double quotes (\"\")."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`int()`会将小数点后所有的内容舍弃掉！\n",
    "\n",
    "字符串数据类型允许你将一段文本作为变量进行操作.我们用单引号 ('') 或者双引号(\"\")来表示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = 'This is a string with single quotes'\n",
    "print(my_string)\n",
    "my_string = \"This is a string with double quotes\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both are allowed so that we can include apostrophes or quotation marks in a string if we so choose."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "两种方式都是允许的,这是为了能让我们灵活的在字符串中插入单引号或者双引号."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = '\"Jabberwocky\", by Lewis Carroll'\n",
    "print(my_string)\n",
    "my_string = \"'Twas brillig, and the slithy toves / Did gyre and gimble in the wabe;\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Booleans, or `bools` are binary variable types. A `bool` can only take on one of two values, these being `True` or `False`. There is much more to this idea of truth values when it comes to programming, which we cover later in the [Logical Operators](#id-section5) of this notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "布尔值是一种二进制变量类型.一个`bool`型变量的值只能是`True`或者`False`.在接下来的[Logical Operators](#id-section5)章节中会介绍更多关于真值的概念."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_bool = True\n",
    "print(my_bool, type(my_bool))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many more data types that you can assign as variables in Python, but these are the basic ones! We will cover a few more later as we move through this tutorial."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在Python中有非常多的数据类型可供使用,这里的只是一些基础！随着课程的进行,我们将逐渐接触其他的数据类型."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Math\n",
    "\n",
    "Python has a number of built-in math functions. These can be extended even further by importing the **math** package or by including any number of other calculation-based packages.\n",
    "\n",
    "All of the basic arithmetic operations are supported: `+`, `-`, `/`, and `*`. You can create exponents by using `**` and modular arithmetic is introduced with the mod operator, `%`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数学基础\n",
    "Python有内置的数值计算函数,通过导入`math`或者其他的计算包来丰富你的数值计算函数.\n",
    "\n",
    "所有基础的算术操作包括`+`, `-`, `/`, 和 `*`.你可以用`**`表示幂运算,`%`表示模运算."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('Addition: ', 2 + 2)\n",
    "print('Subtraction: ', 7 - 4)\n",
    "print('Multiplication: ', 2 * 5)\n",
    "print('Division: ', 10 / 2)\n",
    "print('Exponentiation: ', 3**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you are not familiar with the the mod operator, it operates like a remainder function. If we type $15 \\ \\% \\ 4$, it will return the remainder after dividing $15$ by $4$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你对模运算不熟悉, 可以把他想象成一个求余数的函数. 当我们运行 $15 \\ \\% \\ 4$, 他会返回 $15$ 除以 $4$ 的余数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('Modulo: ', 15 % 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mathematical functions also work on variables!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数学函数同样能作用在变量上！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "first_integer = 4\n",
    "second_integer = 5\n",
    "print(first_integer * second_integer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure that your variables are floats if you want to have decimal points in your answer. If you perform math exclusively with integers, you get an integer. Including any float in the calculation will make the result a float."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 如果你想要答案有小数点,请确保你的变量类型是浮点型.\n",
    "- 如果是用整数执行计算,得到的则会是整数.(**这个法则仅仅在python 2中生效!!**)\n",
    "- 如果计算中有一个是浮点数,那么结果也会是浮点数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "first_integer = 11\n",
    "second_integer = 3\n",
    "\n",
    "# 当你的python的版本是2.\n",
    "# print first_integer / second_integer\n",
    "print(first_integer // second_integer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "first_number = 11.0\n",
    "second_number = 3.0\n",
    "print(first_number / second_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python has a few built-in math functions. The most notable of these are:\n",
    "\n",
    "* `abs()`\n",
    "* `round()`\n",
    "* `max()`\n",
    "* `min()`\n",
    "* `sum()`\n",
    "\n",
    "These functions all act as you would expect, given their names. Calling `abs()` on a number will return its absolute value. The `round()` function will round a number to a specified number of the decimal points (the default is $0$). Calling `max()` or `min()` on a collection of numbers will return, respectively, the maximum or minimum value in the collection. Calling `sum()` on a collection of numbers will add them all up. If you're not familiar with how collections of values in Python work, don't worry! We will cover collections in-depth in the next section. \n",
    "\n",
    "Additional math functionality can be added in with the `math` package."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python有一些内置的数学函数,其中最为常见的:\n",
    "\n",
    "* `abs()`\n",
    "* `round()`\n",
    "* `max()`\n",
    "* `min()`\n",
    "* `sum()`\n",
    "\n",
    "这些函数作用就如同字面意思上. 调用 `abs()` 会返回一个数字绝对值.  `round()`函数会将数字舍入到指定的小数点数 (默认值为 0). 对一个数字集合调用 `max()` 或者 `min()`将会返回集合中的最大值或者最小值. 对于一个数字集合调用 `sum()` 将返回其总和.如果你不太清楚Python中的集合工作原理,不用担心,我们会将在下一节深入探讨.\n",
    "\n",
    "使用`math`包来导入更多的数学函数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The math library adds a long list of new mathematical functions to Python. Feel free to check out the [documentation](https://docs.python.org/2/library/math.html) for the full list and details. It concludes some mathematical constants"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`math`库添加了很多新的数学函数,通过查看[documentation](https://docs.python.org/2/library/math.html),获取完整列表和详细信息,其中包含了数学常数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('Pi: ', math.pi)\n",
    "print(\"Euler's Constant: \", math.e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As well as some commonly used math functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以及一些常用的数学函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('Cosine of pi: ', math.cos(math.pi))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Collections\n",
    "### Lists\n",
    "\n",
    "A `list` in Python is an ordered collection of objects that can contain any data type. We define a `list` using brackets (`[]`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合\n",
    "### List (列表)\n",
    "\n",
    "Python中的`list`是可以包含任何数据类型的对象的有序集合.\n",
    "我们使用方括号(`[]`)定义一个列表."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3]\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can access and index the list by using brackets as well. In order to select an individual element, simply type the list name followed by the index of the item you are looking for in braces."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以通过方括号来访问和索引列表,为了选择一个单独的元素,只需输入列表名称,然后输入你要查找的括号中的项目的索引."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[0])\n",
    "print(my_list[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Indexing in Python starts from $0$. If you have a list of length $n$, the first element of the list is at index $0$, the second element is at index $1$, and so on and so forth. The final element of the list will be at index $n-1$. Be careful! Trying to access a non-existent index will cause an error."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python的索引起始值是$0$.如果你列表的长度是$n$, 第一个元素的索引号是$0$, 第二个元素的索引号是$1$,以此类推. 最后一个元素的索引是$n-1$. 一定要小心! 当你尝试访问一个不存在的索引,就会报错."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('The first, second, and third list elements: ', my_list[0], my_list[1], my_list[2])\n",
    "print('Accessing outside the list bounds causes an error: ', my_list[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see the number of elements in a list by calling the `len()` function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以调用`len()`函数来获取list的元素个数(列表长度)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(len(my_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can update and change a list by accessing an index and assigning new value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以通过访问索引并赋新值来更新和改变列表."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list)\n",
    "my_list[0] = 42\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is fundamentally different from how strings are handled. A `list` is mutable, meaning that you can change a `list`'s elements without changing the list itself. Some data types, like `strings`, are immutable, meaning you cannot change them at all. Once a `string` or other immutable data type has been created, it cannot be directly modified without creating an entirely new object."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表和字符串的处理有根本的区别.\n",
    "`list`是可变的数据类型,这意味着你可以更改`list`的元素.\n",
    "一些数据类型（如 `strings`）是不可变的,你无法去修改他们.\n",
    "一旦创建了一个`string`或其他不可变数据类型,就不能直接修改它,除非创建一个全新的对象."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = \"Strings never change\"\n",
    "my_string[0] = 'Z'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we stated before, a list can contain any data type. Thus, lists can also contain strings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正如前面所说,列表的元素可以是任何数据类型.因此,字符串也可以作为元素."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list_2 = ['one', 'two', 'three']\n",
    "print(my_list_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lists can also contain multiple different data types at once!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表也能同时容纳不同数据类型的数据."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list_3 = [True, 'False', 42]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to put two lists together, they can be combined with a `+` symbol."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可以用一个`+`来连接两个列表."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list_4 = my_list + my_list_2 + my_list_3\n",
    "print(my_list_4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to accessing individual elements of a list, we can access groups of elements through slicing."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了可以访问列表中单个元素之外,还可以通过切片(slice)的方式访问多个元素."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list = ['friends', 'romans', 'countrymen', 'lend', 'me', 'your', 'ears']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Slicing\n",
    "\n",
    "We use the colon (`:`) to slice lists. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 切片\n",
    "\n",
    "我们使用分号 (`:`)对列表进行切片. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(my_list[2:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `:` we can select a group of elements in the list starting from the first element indicated and going up to  (but not including) the last element indicated.\n",
    "\n",
    "We can also select everything after a certain point"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`:`左边是切片的起点索引,右边是切片终点索引,顾名思义,就是选取起点和终点之间的值,包含起点,单不包含终点.\n",
    "类似数学中开闭集合:[start, end)\n",
    "\n",
    "如果省略`:`左边的索引,则代表从第0个元素开始选取,类似的,省略右边的索引,则选取到列表最后一个元素."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(my_list[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And everything before a certain point"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "某个元素之前所有的点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(my_list[:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using negative numbers will count from the end of the indices instead of from the beginning. For example, an index of `-1` indicates the last element of the list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用负数索引将会从末尾倒序计数.例如,索引为`-1`表示列表最后一个元素."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also add a third component to slicing. Instead of simply indicating the first and final parts of your slice, you can specify the step size that you want to take. So instead of taking every single element, you can take every other element."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设一个列表为a,a切片的一般形式为:\n",
    "`a[start : end : step]`    \n",
    " start是切片起点索引，end是切片终点索引，但切片结果不包括终点索引的值。step是步长默认是1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[0:7:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have selected the entire list (because `0:7` will yield elements `0` through `6`) and we have selected a step size of `2`. So this will spit out element `0` , element `2`, element `4`, and so on through the list element selected. We can skip indicated the beginning and end of our slice, only indicating the step, if we like."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们选择了整个列表(因为0:7包含了0到6所有的元素),并且我们指定单步长为2.所以这将通过列表元素返回元素0,元素2,元素4等等.如果我们愿意,我们可以忽略开始位置和结束位置,仅指示步长."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lists implicitly select the beginning and end of the list when not otherwise specified."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "没有特别说明的话,列表的切片是默认指定开头和结束位置的."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With a negative step size we can even reverse the list!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用一个负数作为步长,我们可以对列表进行逆向排序！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_list[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python does not have native matrices, but with lists we can produce a working fascimile. Other packages, such as `numpy`, add matrices as a separate data type, but in base Python the best way to create a matrix is to use a list of lists."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python没有原生的矩阵数据类型,但是我们使用列表可以达到类似的效果.另外一些包,比如`numpy`,把矩阵添加为单独的数据类型,但是在Python基础应用中,使用列表嵌套的方式创建矩阵是一个不错的选择.\n",
    "\n",
    "- 译者の疑问：fascimile该如何翻译? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use built-in functions to generate lists. In particular we will look at `range()` (because we will be using it later!). Range can take several different inputs and will return a list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们也能通过使用内置函数来生成列表.我们会着重来看一下`range()`.`range`能采取几种不同的参数输入方式,并且返回一个列表(注意其返回的数据类型并不是`list`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b = 10\n",
    "my_list = range(b)\n",
    "print(my_list)\n",
    "print(type(my_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar to our list-slicing methods from before, we can define both a start and an end for our range. This will return a list that is includes the start and excludes the end, just like a slice."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类似列表切片方法,对于`range`我们也可以指定开始和结束位置,返回一个列表,并且包含开头但不包含结尾,这一点也和切片的原理类似."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = 0\n",
    "b = 10\n",
    "my_list = range(a, b)\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also specify a step size. This again has the same behavior as a slice."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类似切片,我们一样可以设定一个步长."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a = 0\n",
    "b = 10\n",
    "step = 2\n",
    "my_list = range(a, b, step)\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tuples\n",
    "\n",
    "A `tuple` is a data type similar to a list in that it can hold different kinds of data types. The key difference here is that a `tuple` is immutable. We define a `tuple` by separating the elements we want to include by commas. It is conventional to surround a `tuple` with parentheses."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tuples(元组)\n",
    "`tuple`是类似于列表的数据类型,它可以容纳不同类型的数据类型.\n",
    "这里最关键不同点是`tuple`是不可变的.\n",
    "我们通过用逗号分隔我们要包括的元素来定义一个`tuple`.\n",
    "通常用圆括号来表示一个`tuple`.\n",
    "\n",
    "注意:当你定义一个只有一个元素的`tuple`时,需要带上`','`,请感受下`(1)`和`(1,)`,计算机会把哪一个当做`tuple`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_tuple = 'I', 'have', 30, 'cats'\n",
    "print(my_tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As mentioned before, tuples are immutable. You can't change any part of them without defining a new tuple."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如前所述,元组是不可变的.\n",
    "在不定义新元组的情况下,你不能修改他的元素."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 强行将喵星人换成doge,是会遭天谴的！！！不信抬头看,苍天饶过谁！！！\n",
    "my_tuple[3] = 'dogs' # Attempts to change the 'cats' value stored in the the tuple to 'dogs'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can slice tuples the same way that you slice lists!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同样的你也可以像list那样使用切片！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_tuple[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And concatenate them the way that you would with strings!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "并且能像`strings`那样把他们连接起来！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_other_tuple = ('make', 'that', 50)\n",
    "print(my_tuple + my_other_tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can 'pack' values together, creating a tuple (as above), or we can 'unpack' values from a tuple, taking them out."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以将一些值'压缩'在一起组成一个元组,我们也能通过'解压缩'的方式,把值从元组里取出来."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "str_1, str_2, int_1 = my_other_tuple\n",
    "print(str_1, str_2, int_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unpacking assigns each value of the tuple in order to each variable on the left hand side of the equals sign. Some functions, including user-defined functions, may return tuples, so we can use this to directly unpack them and access the values that we want."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "'解压缩'赋值是将元组里的元素按照顺序依次传递给等号左边的变量.有一些函数,包括用户自定义的函数,都可以返回元组,因此我们可以用这种'解压缩'的方式来获取我们想要的值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sets\n",
    "\n",
    "A `set` is a collection of unordered, unique elements. It works almost exactly as you would expect a normal set of things in mathematics to work and is defined using braces (`{}`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sets(集合)\n",
    "\n",
    "`set`是无序且元素唯一的集合.\n",
    "它几乎完全和数学中定义一样,并使用花括号(`{}`)来表示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "things_i_like = {'dogs', 7, 'the number 4', 4, 4, 4, 42, 'lizards', 'man I just LOVE the number 4'}\n",
    "print(things_i_like, type(things_i_like))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note how any extra instances of the same item are removed in the final set. We can also create a `set` from a list, using the `set()` function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以使用`set()`函数,来剔除掉list中的重复元素并且返回一个`set`数据类型."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "animal_list = ['cats', 'dogs', 'dogs', 'dogs', 'lizards', 'sponges', 'cows', 'bats', 'sponges']\n",
    "animal_set = set(animal_list)\n",
    "print(animal_set) # Removes all extra instances from the list 从list中删除重复的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calling `len()` on a set will tell you how many elements are in it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同样的使用`len()`函数可以告诉我们有多少个元素在set中."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(len(animal_set))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because a `set` is unordered, we can't access individual elements using an index. We can, however, easily check for membership (to see if something is contained in a set) and take the unions and intersections of sets by using the built-in set functions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于`set`是无序的,我们不能通过索引来访问某个单独的元素.但是我们能简单验证他的元素构成(去判断某个元素是否在其中)并使用内置方法来求集合的交集和并集."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'cats' in animal_set # Here we check for membership using the `in` keyword."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we checked to see whether the string 'cats' was contained within our `animal_set` and it returned `True`, telling us that it is indeed in our set.\n",
    "\n",
    "We can connect sets by using typical mathematical set operators, namely `|`, for union, and `&`, for intersection. Using `|` or `&` will return exactly what you would expect if you are familiar with sets in mathematics."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这里,我们检查了'cats'字符串是否包含在我们的`animal_set`中,并返回`True`,告诉我们它的确在我们的集合中.我们可以使用经典的数学集合运算符来进行集合操作,`|`,表示取并集操作,`&`表示取交集操作."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(animal_set | things_i_like) # 交换两者的位置 things_i_like | animal_set 并没有什么区别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pairing two sets together with `|` combines the sets, removing any repetitions to make every set element unique."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`|`运算符可以求得两个集合的并集,并且移除重复的元素,使其唯一."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(animal_set & things_i_like) # You can also write things_i_like & animal_set with no difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pairing two sets together with `&` will calculate the intersection of both sets, returning a set that only contains what they have in common.\n",
    "\n",
    "If you are interested in learning more about the built-in functions for sets, feel free to check out the [documentation](https://docs.python.org/2/library/sets.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 `&` 运算符,可以取得两个集合的交集:返回两个集合中共有的元素.\n",
    "\n",
    "如果你对sets的内置函数有兴趣, 可以 [点我查看文档](https://docs.python.org/2/library/sets.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dictionaries\n",
    "\n",
    "Another essential data structure in Python is the dictionary. Dictionaries are defined with a combination of curly braces (`{}`) and colons (`:`). The braces define the beginning and end of a dictionary and the colons indicate key-value pairs. A dictionary is essentially a set of key-value pairs. The key of any entry must be an immutable data type. This makes both strings and tuples candidates. Keys can be both added and deleted.\n",
    "\n",
    "In the following example, we have a dictionary composed of key-value pairs where the key is a genre of fiction (`string`) and the value is a list of books (`list`) within that genre. Since a collection is still considered a single entity, we can use one to collect multiple variables or values into one key-value pair."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典\n",
    "\n",
    "字典,另一个在python十分有用的数据结构.字典使用花括号(`{}`)和分号(`:`)来表示.括号定义了一个字典起始和结束,分号用于表示键-值对(key-value pair). 字典其实是键值对的集合(`set`).`key`可以是任何不可变的数据类型,`strings`和`tuples`都可以作为`key`.`key`是可以添加和删除的.\n",
    "\n",
    "在下面的示例中,我们有一个由键值对组成的字典,其中小说类型(`string`)是`key`,该类型中的书籍列表(`list`)是`value`.可以把书籍列表视为单个实体,我们就可以把小说和书籍列表构成一个键值对."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_dict = {\"High Fantasy\": [\"Wheel of Time\", \"Lord of the Rings\"], \n",
    "           \"Sci-fi\": [\"Book of the New Sun\", \"Neuromancer\", \"Snow Crash\"],\n",
    "           \"Weird Fiction\": [\"At the Mountains of Madness\", \"The House on the Borderland\"]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After defining a dictionary, we can access any individual value by indicating its key in brackets."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义一个字典后,我们能通过方括号使用`key`作为索引,来访问对应的`value`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_dict[\"Sci-fi\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also change the value associated with a given key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们也能通过给定`key`来改变`value`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_dict[\"Sci-fi\"] = \"I can't read\"\n",
    "print(my_dict[\"Sci-fi\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adding a new key-value pair is as simple as defining it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以很轻松的添加键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_dict[\"Historical Fiction\"] = [\"Pillars of the Earth\"]\n",
    "print(my_dict[\"Historical Fiction\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(my_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String Shenanigans\n",
    "\n",
    "We already know that strings are generally used for text. We can used built-in operations to combine, split, and format strings easily, depending on our needs.\n",
    "\n",
    "The `+` symbol indicates concatenation in string language. It will combine two strings into a longer string."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串的小技巧\n",
    "\n",
    "我们已经知道字符串通常用于文本.根据我们的需要,我们可以使用内置操作轻松组合,拆分和格式化字符串. \n",
    "\n",
    "`+` 表示字符串语言的连接.它将两个字符串组合成一个更长的字符串."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "first_string = '\"Beware the Jabberwock, my son! /The jaws that bite, the claws that catch! /'\n",
    "second_string = 'Beware the Jubjub bird, and shun /The frumious Bandersnatch!\"/'\n",
    "third_string = first_string + second_string\n",
    "print(third_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Strings are also indexed much in the same way that lists are."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字符串能够像列表那样进行索引."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = 'Supercalifragilisticexpialidocious'\n",
    "print('The first letter is: ', my_string[0]) # Uppercase S\n",
    "print('The last letter is: ', my_string[-1]) # lowercase s\n",
    "print('The second to last letter is: ', my_string[-2]) # lowercase u\n",
    "print('The first five characters are: ', my_string[0:5]) # Remember: slicing doesn't include the final element!\n",
    "print('Reverse it!: ', my_string[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Built-in objects and classes often have special functions associated with them that are called methods. We access these methods by using a period ('.'). We will cover objects and their associated methods more in another lecture!\n",
    "\n",
    "Using string methods we can count instances of a character or group of characters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内置对象和类通常具有与它们相关联的特殊功能,称为方法.\n",
    "我们通过使用句号('.')调用这些方法.我们将在另一节课中介绍对象及其相关的使用方法！\n",
    "\n",
    "使用字符串方法:`.count`能够对一个或多个字符进行计数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print('Count of the letter i in Supercalifragilisticexpialidocious: ', my_string.count('i'))\n",
    "print('Count of \"li\" in the same word: ', my_string.count('li'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also find the first instance of a character or group of characters in a string."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们也能通过`.find`找到字符在字符串中第一次出现的位置."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('The first time i appears is at index: ', my_string.find('i'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As well as replace characters in a string."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以及字符串的替换:`.replace`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(\"All i's are now a's: \", my_string.replace('i', 'a'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(\"It's raining cats and dogs\".replace('dogs', 'more cats'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are also some methods that are unique to strings. The function `upper()` will convert all characters in a string to uppercase, while `lower()` will convert all characters in a string to lowercase!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里也有字符串特有的方法.比如函数`upper()`,将所有的字符转化为大写；类似的, `lower()` 讲字符转化成小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = \"I can't hear you\"\n",
    "print(my_string.upper())\n",
    "my_string = \"I said HELLO\"\n",
    "print(my_string.lower())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### String Formatting\n",
    "\n",
    "Using the `format()` method we can add in variable values and generally format our strings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串格式化输出\n",
    "\n",
    "用`format`方法,我们能变量的值进行格式化后输出成文本."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = \"{0} {1}\".format('Marco', 'Polo')\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = \"{1} {0}\".format('Marco', 'Polo')\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use braces (`{}`) to indicate parts of the string that will be filled in later and we use the arguments of the `format()` function to provide the values to substitute. The numbers within the braces indicate the index of the value in the `format()` arguments."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们使用花括号(`{}`)来表示稍后将被填充的字符串的部分,我们使用`format()`函数的参数来提供替换的值.\n",
    "大括号中的数字表示`format()`参数中的值的索引."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See the `format()` [documentation](https://docs.python.org/2/library/string.html#format-examples) for additional examples."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以参考 `format()` [文档](https://docs.python.org/2/library/string.html#format-examples) 查看更多示例."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you need some quick and dirty formatting, you can instead use the `%` symbol, called the string formatting operator. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你需要一种快餐化的格式化输出,你可以用`%`来进行操作,这被称为字符串格式化运算符."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('insert %s here' % 'value')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `%` symbol basically cues Python to create a placeholder. Whatever character follows the `%` (in the string) indicates what sort of type the value put into the placeholder will have. This character is called a *conversion type*. Once the string has been closed, we need another `%` that will be followed by the values to insert. In the case of one value, you can just put it there. If you are inserting more than one value, they must be enclosed in a tuple."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%`符号告诉Python创建一个占位符. `%`(在字符串中)后面的任何字符表示占位符所输入的值的类型.这个字符称为*conversion type*(*转换类型*).一旦字符串被关闭,我们需要另外一个`%`,后面跟随有值插入.在一个值的情况下,你可以把它放在那里.如果要插入多个值,则必须将置于元组中."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print('There are %s cats in my %s' % (13, 'apartment'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In these examples, the `%s` indicates that Python should convert the values into strings. There are multiple conversion types that you can use to get more specific with the the formatting. See the string formatting [documentation](https://docs.python.org/2/library/stdtypes.html#string-formatting) for additional examples and more complete details on use."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这些示例中,`%s`表示Python应该将值转换为字符串.您可以使用多种转换类型来使用格式进行更具体化. 详见字符串格式化 [文档](https://docs.python.org/2/library/stdtypes.html#string-formatting) 这里有更多的示例和更完整的细节以供参考."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Logical Operators\n",
    "### Basic Logic\n",
    "\n",
    "Logical operators deal with `boolean` values, as we briefly covered before. If you recall, a `bool` takes on one of two values, `True` or `False` (or $1$ or $0$). The basic logical statements that we can make are defined using the built-in comparators. These are `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal to), and `>=` (greater than or equal to)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逻辑运算符\n",
    "### 基础的逻辑运算符\n",
    "\n",
    "逻辑运算符主要处理的是`boolean`变量,就像我们之前提过的那样.如果你还记得,`bool`只能是`True`或者 `False`(or $1$ or $0$).我们可以使用内置的比较运算符来定义基本的逻辑语句.这里有 `==` (等于号), `!=` (不等号), `<` (小于号), `>` (大于号), `<=` (小于等于号), 和 `>=` (大于等于号)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(5 == 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(5 > 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These comparators also work in conjunction with variables."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这些比较运算符同样能作用于变量."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "m = 2\n",
    "n = 23\n",
    "print(m < n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can string these comparators together to make more complex logical statements using the logical operators `or`, `and`, and `not`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以将这些比较运算符进行组合,以表现更复杂的逻辑语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "statement_1 = 10 > 2\n",
    "statement_2 = 4 <= 6\n",
    "print(\"Statement 1 truth value: {0}\".format(statement_1))\n",
    "print(\"Statement 2 truth value: {0}\".format(statement_2))\n",
    "print(\"Statement 1 and Statement 2: {0}\".format(statement_1 and statement_2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `or` operator performs a logical `or` calculation. This is an inclusive `or`, so if either component paired together by `or` is `True`, the whole statement will be `True`. The `and` statement only outputs `True` if all components that are `and`ed together are True. Otherwise it will output `False`. The `not` statement simply inverts the truth value of whichever statement follows it. So a `True` statement will be evaluated as `False` when a `not` is placed in front of it. Similarly, a `False` statement will become `True` when a `not` is in front of it.\n",
    "\n",
    "Say that we have two logical statements, or assertions, $P$ and $Q$. The truth table for the basic logical operators is as follows:\n",
    "\n",
    "|  P  |  Q  | `not` P| P `and` Q | P `or` Q|\n",
    "|:-----:|:-----:|:---:|:---:|:---:|\n",
    "| `True` | `True` | `False` | `True` | `True` |\n",
    "| `False` | `True` | `True` | `False` | `True` |\n",
    "| `True` | `False` | `False` | `False` | `True` |\n",
    "| `False` | `False` | `True` | `False` | `False` |\n",
    "\n",
    "We can string multiple logical statements together using the logical operators."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`or`操作符执行逻辑 `或` 运算. This is an inclusive `or`, `or`连接的语句中有一个为`True`, 那么整个语句为`True`. `and`语句为`True`当且仅当所有`and`连接的语句全为`True`. 否则就输出`False`.  `not`语句只是简单地反转语句的真值. 所以一个 `True`语句经过`not`计算后变成 `False`. 相似的, 一个 `False` 语句经过`not`计算后变成`True`.\n",
    "\n",
    "比如这里有两个逻辑语句, 或者叫断言, $P$ 和 $Q$. 简单逻辑运算的真值表如下:\n",
    "\n",
    "|  P  |  Q  | `not` P| P `and` Q | P `or` Q|\n",
    "|:-----:|:-----:|:---:|:---:|:---:|\n",
    "| `True` | `True` | `False` | `True` | `True` |\n",
    "| `False` | `True` | `True` | `False` | `True` |\n",
    "| `True` | `False` | `False` | `False` | `True` |\n",
    "| `False` | `False` | `True` | `False` | `False` |\n",
    "\n",
    "我们可以使用逻辑运算符将多个逻辑语句串在一起."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(((2 < 3) and (3 > 0)) or ((5 > 6) and not (4 < 2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Logical statements can be as simple or complex as we like, depending on what we need to express. Evaluating the above logical statement step by step we see that we are evaluating (`True and True`) `or` (`False and not False`). This becomes `True or (False and True`), subsequently becoming `True or False`, ultimately being evaluated as `True`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逻辑语句可以很简单,也可以很复杂,这个取决于我们如何去表达.Logical statements can be as simple or complex as we like, depending on what we need to express. 上述的逻辑语句我们可以一步步去计算,首先计算最里层括号里的逻辑语句: (`True and True`) `or` (`False and not False`). 接下来: `True or (False and True`), 依次计算得到: `True or False`, 最终就会变成 `True`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Truthiness\n",
    "\n",
    "Data types in Python have a fun characteristic called truthiness. What this means is that most built-in types will evaluate as either `True` or `False` when a boolean value is needed (such as with an if-statement). As a general rule, containers like strings, tuples, dictionaries, lists, and sets, will return `True` if they contain anything at all and `False` if they contain nothing."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Truthiness\n",
    "\n",
    "在Python中的数据类型有一种特性叫做`Truthiness`.当我们需要进行布尔值判断时,大多数内置数据类型都可以进行逻辑判断(比如一个if语句).通用的规则是,比如像字符串,元组,字典,列表和集合,只要里面元素个数不为0,则返回`Ture`,否则返回`False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Similar to how float() and int() work, bool() forces a value to be considered a boolean!\n",
    "print(bool(''))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(bool('I have character!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(bool([]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(bool([1, 2, 3]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And so on, for the other collections and containers. `None` also evaluates as `False`. The number `1` is equivalent to `True` and the number `0` is equivalent to `False` as well, in a boolean context."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外,对于其他的`collections`和`containers`,在需要判断布尔值的情况下,`None`也视为`False`.数字`1`视为`True`,数字`0`视为`False`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If-statements\n",
    "\n",
    "We can create segments of code that only execute if a set of conditions is met. We use if-statements in conjunction with logical statements in order to create branches in our code. \n",
    "\n",
    "An `if` block gets entered when the condition is considered to be `True`. If condition is evaluated as `False`, the `if` block will simply be skipped unless there is an `else` block to accompany it. Conditions are made using either logical operators or by using the truthiness of values in Python. An if-statement is defined with a colon and a block of indented text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If-statements(If语句)\n",
    "\n",
    "我们可以创建只在满足一定条件时执行的代码段.我们将if语句与逻辑语句相结合,以便在代码中创建分支.\n",
    "\n",
    "当条件被认为是`True`时,我们则进入`if`分支.当条件被认为是`False`时,我们则会忽略`if`分支,直到进入`else`分支.\n",
    "在Python中用逻辑语句或者变量的真值作为条件进行判断,if语句用冒号和一段缩进文本定义."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# This is the basic format of an if statement. This is a vacuous example. \n",
    "# The string \"Condition\" will always evaluated as True because it is a\n",
    "# non-empty string. he purpose of this code is to show the formatting of\n",
    "# an if-statement.\n",
    "if \"Condition\": \n",
    "    # This block of code will execute because the string is non-empty\n",
    "    # Everything on these indented lines\n",
    "    print(True)\n",
    "else:\n",
    "    # So if the condition that we examined with if is in fact False\n",
    "    # This block of code will execute INSTEAD of the first block of code\n",
    "    # Everything on these indented lines\n",
    "    print(False)\n",
    "# The else block here will never execute because \"Condition\" is a non-empty string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 4\n",
    "if i == 5:\n",
    "    print('The variable i has a value of 5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because in this example `i = 4` and the if-statement is only looking for whether `i` is equal to `5`, the print statement will never be executed. We can add in an `else` statement to create a contingency block of code in case the condition in the if-statement is not evaluated as `True`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个示例中`i = 4`,if语句只是判断`i`是否等于`5`,所以print语句永远不会执行.为了应对这种if条件判断永远不为`True`的情况下,我们可以使用`else`语句创建一个代码块应对这种情况."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 4\n",
    "if i == 5:\n",
    "    print(\"All lines in this indented block are part of this block\")\n",
    "    print('The variable i has a value of 5')\n",
    "else:\n",
    "    print(\"All lines in this indented block are part of this block\")\n",
    "    print('The variable i is not equal to 5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can implement other branches off of the same if-statement by using `elif`, an abbreviation of \"else if\". We can include as many `elifs` as we like until we have exhausted all the logical branches of a condition."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以添加和if语句功能一样的`elif`语句,他是`else if`的缩写.我们能添加任意多个`elifs`语句,直到我们穷尽了所有逻辑分支条件."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 1\n",
    "if i == 1:\n",
    "    print('The variable i has a value of 1')\n",
    "elif i == 2:\n",
    "    print('The variable i has a value of 2')\n",
    "elif i == 3:\n",
    "    print('The variable i has a value of 3')\n",
    "else:\n",
    "    print(\"I don't care what i is\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also nest if-statements within if-statements to check for further conditions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果是多条件判断,我们可以使用嵌套形式的if语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 10\n",
    "if i % 2 == 0:\n",
    "    if i % 3 == 0:\n",
    "        print('i is divisible by both 2 and 3! Wow!')\n",
    "    elif i % 5 == 0:\n",
    "        print('i is divisible by both 2 and 5! Wow!')\n",
    "    else:\n",
    "        print('i is divisible by 2, but not 3 or 5. Meh.')\n",
    "else:\n",
    "    print('I guess that i is an odd number. Boring.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember that we can group multiple conditions together by using the logical operators!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请记住,我们同样能逻辑操作符进行多条件判断！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 5\n",
    "j = 12\n",
    "if i < 10 and j > 11:\n",
    "    print('{0} is less than 10 and {1} is greater than 11! How novel and interesting!'.format(i, j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use the logical comparators to compare strings!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你能用逻辑操作符来比较字符串！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_string = \"Carthago delenda est\"\n",
    "if my_string == \"Carthago delenda est\":\n",
    "    print('And so it was! For the glory of Rome!')\n",
    "else:\n",
    "    print('War elephants are TERRIFYING. I am staying home.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As with other data types, `==` will check for whether the two things on either side of it have the same value. In this case, we compare whether the value of the strings are the same. Using `>` or `<` or any of the other comparators is not quite so intuitive, however, so we will stay from using comparators with strings in this lecture. Comparators will examine the [lexicographical order](https://en.wikipedia.org/wiki/Lexicographical_order) of the strings, which might be a bit more in-depth than you might like."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于其他数据类型, `==`同样能检测等号两边参数值是否相等. 在这个示例中,我们比较两个字符串的值是否相等. 然而对于 `>` 或者 `<` 或者其他比较操作符就不是那么直观了,因此在本次课程中我们将继续用字符串作为比较操作符的操作对象. 如果你想更深入了解下字符串的比较操作可以查阅 [lexicographical order](https://en.wikipedia.org/wiki/Lexicographical_order) ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some built-in functions return a boolean value, so they can be used as conditions in an if-statement. User-defined functions can also be constructed so that they return a boolean value. This will be covered later with function definition!\n",
    "\n",
    "The `in` keyword is generally used to check membership of a value within another value. We can check memebership in the context of an if-statement and use it to output a truth value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一些内置函数会返回布尔值,所以他们也可以被当做if语句的判断条件使用.能返回布尔值的用户自定义函数也可以这样操作.我们在后面会介绍自定义函数！\n",
    "\n",
    "`in`关键字通常用于检查一个值在另一个值中的成员关系.我们可以把这个成员检查的结果作为if语句条件判断."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if 'a' in my_string or 'e' in my_string:\n",
    "    print('Those are my favorite vowels!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we use `in` to check whether the variable `my_string` contains any particular letters. We will later use `in` to iterate through lists!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们使用`in`检查变量`my_string`是否包含某个特定字母在里面.稍后我们会使用`in`对list进行循环."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loop Structures\n",
    "\n",
    "Loop structures are one of the most important parts of programming. The `for` loop and the `while` loop provide a way to repeatedly run a block of code repeatedly. A `while` loop will iterate until a certain condition has been met. If at any point after an iteration that condition is no longer satisfied, the loop terminates. A `for` loop will iterate over a sequence of values and terminate when the sequence has ended. You can instead include conditions within the `for` loop to decide whether it should terminate early or you could simply let it run its course."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loop Structures(循环结构)\n",
    "\n",
    "循环结构是程序设计的重要组成部分.`for`循环和`while`循环提供了重复运行一段代码的方法.一个`while`循环将不断迭代直到某个条件满足为止.如果在迭代之后,条件不再满足,则循环终止. 一个`for`循环将在一系列值上遍历,并在序列结束时终止.相反的,你可以在 `for`循环中利用一些条件提前中止,或者可以让他简单的运行(You can instead include conditions within the `for` loop to decide whether it should terminate early or you could simply let it run its course.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 5\n",
    "while i > 0: # We can write this as 'while i:' because 0 is False!\n",
    "    i -= 1\n",
    "    print('I am looping! {0} more to go!'.format(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "With `while` loops we need to make sure that something actually changes from iteration to iteration so that that the loop actually terminates. In this case, we use the shorthand `i -= 1` (short for `i = i - 1`) so that the value of `i` gets smaller with each iteration. Eventually `i` will be reduced to `0`, rendering the condition `False` and exiting the loop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在`while`循环中,我们必须确保循环的判断条件会随着迭代而改变,这是为了避免无限死循环.在这个示例总,我们通过`i-=1`(i = i - 1的缩写),使得每次迭代后,`i`的值越来越小.当`i`等于0时,使得循环变为`False`,然后我们可以退出循环."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `for` loop iterates a set number of times, determined when you state the entry into the loop. In this case we are iterating over the list returned from `range()`. The `for` loop selects a value from the list, in order, and temporarily assigns the value of `i` to it so that operations can be performed with the value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当进入`for`循环后,会迭代一定次数.在这个示例中,我们通过`range`返回的列表进行迭代.每一次迭代,`for`循环会从列表中选择一个值赋值给`i`,以便后面用此值进行计算."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    print('I am looping! I have looped {0} times!'.format(i + 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that in this `for` loop we use the `in` keyword. Use of the `in` keyword is not limited to checking for membership as in the if-statement example. You can iterate over any collection with a `for` loop by using the `in` keyword.\n",
    "\n",
    "In this next example, we will iterate over a `set` because we want to check for containment and add to a new set."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个`for`循环中,我们使用了`in`关键字.`in`关键字不光能用于if语句中作条件判断,也能在`for`循环中,遍历任何集合.\n",
    "\n",
    "在下一个示例中,我们将会对一个`set`进行遍历,我们希望检测是否包含某个成员,如果在则添加到新的集合中."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_list = {'cats', 'dogs', 'lizards', 'cows', 'bats', 'sponges', 'humans'} # Lists all the animals in the world\n",
    "mammal_list = {'cats', 'dogs', 'cows', 'bats', 'humans'} # Lists all the mammals in the world\n",
    "my_new_list = set()\n",
    "for animal in my_list:\n",
    "    if animal in mammal_list:\n",
    "        # This adds any animal that is both in my_list and mammal_list to my_new_list\n",
    "        my_new_list.add(animal)\n",
    "        \n",
    "print(my_new_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two statements that are very helpful in dealing with both `for` and `while` loops. These are `break` and `continue`. If `break` is encountered at any point while a loop is executing, the loop will immediately end."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`break` 和 `continue`语句在循环语句中十分有用.当处于一个循环中,`break`语句被执行,循环会立即中止."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 10\n",
    "while True:\n",
    "    if i == 14:\n",
    "        break\n",
    "    i += 1 # This is shorthand for i = i + 1. It increments i with each iteration.\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    if i == 2:\n",
    "        break\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `continue` statement will tell the loop to immediately end this iteration and continue onto the next iteration of the loop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`continue`语句告诉循环当前的迭代要立即结束并进入到下一个迭代."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "i = 0\n",
    "while i < 5:\n",
    "    i += 1\n",
    "    if i == 3:\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This loop skips printing the number $3$ because of the `continue` statement that executes when we enter the if-statement. The code never sees the command to print the number $3$ because it has already moved to the next iteration. The `break` and `continue` statements are further tools to help you control the flow of your loops and, as a result, your code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个例子中循环没有输出数字3,因为当我if语句条件为真时,`continue`语句被执行,直接进入到下一个循环了,因此当程序执行的时候,'没看见过'这个`print`代码.`break` 和 `continue` 语句能进一步帮你掌控循环的走向,比如结果,代码等等."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The variable that we use to iterate over a loop will retain its value when the loop exits. Similarly, any variables defined within the context of the loop will continue to exist outside of it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "循环使用的变量在循环退出时保留其值.类似地,定义在循环中的任何变量都将继续存在于它之外."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    loop_string = 'I transcend the loop!'\n",
    "    print('I am eternal! I am {0} and I exist everywhere!'.format(i))\n",
    "\n",
    "print('I persist! My value is {0}'.format(i))\n",
    "print(loop_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also iterate over a dictionary!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们同样能遍历一个字典."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_dict = {'firstname' : 'Inigo', 'lastname' : 'Montoya', 'nemesis' : 'Rugen'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for key in my_dict:\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we just iterate over a dictionary without doing anything else, we will only get the keys. We can either use the keys to get the values, like so:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们遍历一个字典,没有特殊操作的话,我们仅仅是拿到他的`key`,当然我们也可以通过`key`取得对应的`value`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for key in my_dict:\n",
    "    print(my_dict[key])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can use the `.items()` function to get both key and value at the same time."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "或者我们用`.items()`同时取到`key`和`value`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#for key, value in my_dict.iteritems(): # python 2 下可以这样调用\n",
    "for key, value in my_dict.items(): # python 3 是这样的\n",
    "    print(key, ':', value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `.items()` function creates a tuple of each key-value pair and the for loop stores unpacks that tuple into `key, value` on each separate execution of the loop!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在每次循环中,`.items()`函数会以`tuple`的形式返回一个键值对,并且进行解压分别赋值给`key, value`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions\n",
    "\n",
    "A function is a reusable block of code that you can call repeatedly to make calculations, output data, or really do anything that you want. This is one of the key aspects of using a programming language. To add to the built-in functions in Python, you can define your own!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数\n",
    "\n",
    "函数是一个可重复使用的代码块,你可以反复调用它进行计算、输出数据,或者任何你想要做的事情.这就是为什么我们要使用编程语言的原因之一.你可以通过自定义函数,将它添加到Python的内置函数中！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hello_world():\n",
    "    \"\"\" Prints Hello, world! \"\"\"\n",
    "    print('Hello, world!')\n",
    "\n",
    "hello_world()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    hello_world()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Functions are defined with `def`, a function name, a list of parameters, and a colon. Everything indented below the colon will be included in the definition of the function.\n",
    "\n",
    "We can have our functions do anything that you can do with a normal block of code. For example, our `hello_world()` function prints a string every time it is called. If we want to keep a value that a function calculates, we can define the function so that it will `return` the value we want. This is a very important feature of functions, as any variable defined purely within a function will not exist outside of it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数的定义由关键字`def`, 函数名, 一串参数和一个冒号组成.任何在冒号后面缩进的代码都属于函数的主体.\n",
    "\n",
    "我们可以在函数主体代码块中做任何事情.比如,我们的`hello_world()` 函数,每被调用一次,就打印一次字符串.当我们想保留函数中计算的数值,我们可以定义函数,并`return`我们想要传出的任何值.这是函数中一个非常重要的特性,任何在函数中定义的变量都不会存在于函数之外."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def see_the_scope():\n",
    "    in_function_string = \"I'm stuck in here!\"\n",
    "\n",
    "see_the_scope()\n",
    "print(in_function_string) # 你会发现在函数外并不存在 in_function_string 这个变量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " The **scope** of a variable is the part of a block of code where that variable is tied to a particular value. Functions in Python have an enclosed scope, making it so that variables defined within them can only be accessed directly within them. If we pass those values to a return statement we can get them out of the function. This makes it so that the function call returns values so that you can store them in variables that have a greater scope.\n",
    " \n",
    "In this case specifically,including a return statement allows us to keep the string value that we define in the function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在一个代码块中,赋给变量的值只能在该代码块中生效,这个也叫做变量的**作用域(scope)**.在Python中,函数拥有封闭的作用域,这样使得其中定义的变量只能在函数内部被直接访问,当我们把这些变量值传递给一个`return`语句,我们就能把这些变量的值从函数中取出来了.这使得函数调用返回值,以便将它们存储在具有较大作用域的变量中.(比如将函数的返回值赋给函数外的变量,函数外的变量就是有较大作用域的变量)\n",
    " \n",
    "在接下来的例子中,特别的有一个返回语句,能够允许我们保留函数中定义的字符串的值."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def free_the_scope():\n",
    "    in_function_string = \"Anything you can do I can do better!\"\n",
    "    return in_function_string\n",
    "my_string = free_the_scope()\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just as we can get values out of a function, we can also put values into a function. We do this by defining our function with parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "我们既然能从函数取出值,当然也能够给函数喂值.这种操作被称为定义函数的参数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def multiply_by_five(x):\n",
    "    \"\"\" Multiplies an input number by 5 \"\"\"\n",
    "    return x * 5\n",
    "\n",
    "n = 4\n",
    "print(n)\n",
    "print(multiply_by_five(n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we only had one parameter for our function, `x`. We can easily add more parameters, separating everything with a comma."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个示例中,我们的函数有一个参数`x`.我们通过`,`添加更多的参数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def calculate_area(length, width):\n",
    "    \"\"\" Calculates the area of a rectangle \"\"\"\n",
    "    return length * width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l = 5\n",
    "w = 10\n",
    "print('Area: ', calculate_area(l, w))\n",
    "print('Length: ', l)\n",
    "print('Width: ', w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def calculate_volume(length, width, depth):\n",
    "    \"\"\" Calculates the volume of a rectangular prism \"\"\"\n",
    "    return length * width * depth"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to, we can define a function so that it takes an arbitrary number of parameters. We tell Python that we want this by using an asterisk (`*`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们想定义一个可以接受任意数量的参数的函数,可以使用`*`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sum_values(*args):\n",
    "    sum_val = 0\n",
    "    for i in args:\n",
    "        sum_val += i\n",
    "    return sum_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(sum_values(1, 2, 3))\n",
    "print(sum_values(10, 20, 30, 40, 50))\n",
    "print(sum_values(4, 2, 5, 1, 10, 249, 25, 24, 13, 6, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The time to use `*args` as a parameter for your function is when you do not know how many values may be passed to it, as in the case of our sum function. The asterisk in this case is the syntax that tells Python that you are going to pass an arbitrary number of parameters into your function. These parameters are stored in the form of a tuple."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当你不知道有多少参数会被传入到求和函数时,你就可以使用`*args`作为函数的参数.这个例子中的星号是告诉Python你要把任意数量的参数传递给函数的语法.这些参数以元组的形式存储."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def test_args(*args):\n",
    "    print(type(args))\n",
    "\n",
    "test_args(1, 2, 3, 4, 5, 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can put as many elements into the `args` tuple as we want to when we call the function. However, because `args` is a tuple, we cannot modify it after it has been created.\n",
    "\n",
    "The `args` name of the variable is purely by convention. You could just as easily name your parameter `*vars` or `*things`. You can treat the `args` tuple like you would any other tuple, easily accessing `arg`'s values and iterating over it, as in the above `sum_values(*args)` function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当我们调用函数是,只要我们想,可以把任意多的元素放入到`args`元组中.然而,因为`args`是一个元组,当他创建之后,是不能修改的.\n",
    "\n",
    "`args`纯粹是约定俗成的名字,你可以叫任何名字`*vars` 或者 `*things`.你可以把`args`就当做普通的元组,很简单的进行访问`arg`的值或者对他进行遍历,也可以像上述`sum_values(*args)`函数那样进行操作."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our functions can return any data type. This makes it easy for us to create functions that check for conditions that we might want to monitor.\n",
    "\n",
    "Here we define a function that returns a boolean value. We can easily use this in conjunction with if-statements and  other situations that require a boolean."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数能返回任何数据类型.这使我们能够轻松地创建我们要想要用于条件判断的函数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def has_a_vowel(word):\n",
    "    \"\"\" \n",
    "    Checks to see whether a word contains a vowel \n",
    "    If it doesn't contain a conventional vowel, it\n",
    "    will check for the presence of 'y' or 'w'. Does\n",
    "    not check to see whether those are in the word\n",
    "    in a vowel context.\n",
    "    \"\"\"\n",
    "    vowel_list = ['a', 'e', 'i', 'o', 'u']\n",
    "    \n",
    "    for vowel in vowel_list:\n",
    "        if vowel in word:\n",
    "            return True\n",
    "    # If there is a vowel in the word, the function returns, preventing anything after this loop from running\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_word = 'catnapping'\n",
    "if has_a_vowel(my_word):\n",
    "    print('How surprising, an english word contains a vowel.')\n",
    "else:\n",
    "    print('This is actually surprising.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def point_maker(x, y):\n",
    "    \"\"\" Groups x and y values into a point, technically a tuple \"\"\"\n",
    "    return x, y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This above function returns an ordered pair of the input parameters, stored as a tuple."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的函数返回一个输入参数的有序对,并作为元组存储."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a = point_maker(0, 10)\n",
    "b = point_maker(5, 3)\n",
    "def calculate_slope(point_a, point_b):\n",
    "    \"\"\" Calculates the linear slope between two points \"\"\"\n",
    "    return (point_b[1] - point_a[1])/(point_b[0] - point_a[0])\n",
    "print(\"The slope between a and b is {0}\".format(calculate_slope(a, b)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that one calculates the slope between two points!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以用来计算两点的斜率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(\"The slope-intercept form of the line between a and b, using point a, is: y - {0} = {2}(x - {1})\".format(a[1], a[0], calculate_slope(a, b)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With the proper syntax, you can define functions to do whatever calculations you want. This makes them an indispensible part of programming in any language."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有了正确的语法,就可以定义函数来完成所需的任何计算.这使得它们成为任何编程语言中不可缺少的一部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next Steps\n",
    "\n",
    "This was a lot of material and there is still even more to cover! Make sure you play around with the cells in each notebook to accustom yourself to the syntax featured here and to figure out any limitations. If you want to delve even deeper into the material, the [documentation for Python](https://docs.python.org/2/) is all available online. We are in the process of developing a second part to this Python tutorial, designed to provide you with even more programming knowledge, so keep an eye on the [Quantopian Lectures Page](quantopian.com/lectures) and the [forums](quantopian.com/posts) for any new lectures."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下一步\n",
    "\n",
    "这里已经有很多材料,但是还有更多的内容需要去了解！请确保你能玩转notebook中每一个单元格中的内容,熟悉里面的语法,找出任何限制.如果你想深入的研究python,可以访问 [Python 3.6.3官方文档](https://docs.python.org/3.6/), [中文文档点我](http://python.usyiyi.cn/translate/python_352/index.html)\n",
    "\n",
    "我们正在开发Python教程的第二部分,旨在为您提供更多的编程知识,请关注 [Quantopian Lectures Page](quantopian.com/lectures)和[论坛](quantopian.com/posts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
